// components/good/good.js
import {
  RedPackets
} from '../../api/redpackets.js'
const redPackets = new RedPackets();
import util from '../../utils/utils'
import {
  HomeHttp
} from '../../api/home.js'
const homeModel = new HomeHttp()
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    show: {
      type: Boolean,
      value: true
    },
    showSubscribeBtn: {
      type: Boolean,
      value: true
    },
    isEnd: {
      type: Boolean,
      value: false
    },
    pize: Object
  },

  /**
   * 组件的初始数据
   */
  data: {
    shareModel: {
      title: '',
      path: '',
      imageUrl: '',
      fromButton: ''
    }
  },
  lifetimes: {
    attached: function() {
      let self = this
      let {
        goods_id,
        name
      } = this.data.pize;
      //获取中奖后的图
      redPackets.getLuckDrawGoods({
        spu_id: goods_id
      }).then((res) => {
        let share = {
          imageUrl: res.img_url,
          title: '正在参加免费抽奖，立即参与',
          path: '/pages/redpackets/redpackets',
          type: 'prize'
        }
        self.setData({
          shareModel: share
        })
      })
    }
  },
  methods: {
    //点击订阅按钮
    onSubscribeBtn() {
      let self = this;
      if (self.data.pize.ifremind != 1) {
        wx.showModal({
          title: '提醒我',
          content: '活动开始前5分钟提醒我',
          confirmColor: '#F93B3B',
          success(res) {
            if (res.confirm) {
              self.httpSetAlert()
            }
          }
        })
      } else {
        self.httpSetAlert()
      }
    },
    //订阅或者取消订阅
    httpSetAlert() {
      homeModel.setAlert({
        "business_type": 2, //业务类型 1 秒杀提醒 ，2 抽奖提醒 ，3 活动提醒
        "business_id": this.data.pize.promot_id, //业务id
      }).then((res) => {
        this.triggerEvent('refData')
      })
    },
    gotoGoodsDetails() {
      wx.navigateTo({
        url: '/pages/goods/mmallGoodsDetails/mmallGoodsDetails?id=' + this.data.pize.goods_id,
      })
    }

  },

});